package com.timothy.android;

import java.util.ArrayList;
import java.util.List;

import com.timothy.chart.CanvasSetting;
import com.timothy.chart.Cylinder;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;

@Deprecated
public class LayoutBAnswerChart extends Activity {
	public final static String Activity_Name = "AndroidCanvasDemoActivity";
	private int screenWidth;
	private int screenHeigth ;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new MyView(this));
		
		WindowManager manage = this.getWindowManager(); 
		Display display = manage.getDefaultDisplay(); 
		screenWidth = display.getWidth(); 
		screenHeigth = display.getHeight();
	}

	class MyView extends SurfaceView implements SurfaceHolder.Callback {
		private SurfaceHolder mHolder;
		private Canvas canvas;

		public MyView(Context context) {
			super(context);
			mHolder = getHolder();
			mHolder.addCallback(this);// 设置Surface生命周期回调
		}

		@Override
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
		}

		@Override
		public void surfaceCreated(SurfaceHolder holder) {
			canvas = mHolder.lockCanvas();// 获得画布
			canvas.drawColor(Color.BLACK);// 设置画布背景为白色
			Paint mPaint = new Paint();
			
			mPaint.setColor(Color.WHITE);		
			canvas.drawText("screenWidth:"+String.valueOf(screenWidth), 50, 50, mPaint); 
			canvas.drawText("screenHeigth:"+String.valueOf(screenHeigth), 50, 75, mPaint); 
			canvas.save();

			//得到所有柱形的信息
			List<Cylinder> cyliList = new ArrayList<Cylinder>();
			Cylinder cylinder1 = new Cylinder(0,250,Color.RED,"Low:250");
			Cylinder cylinder2 = new Cylinder(0,400,Color.YELLOW,"Mid:400");
			Cylinder cylinder3 = new Cylinder(0,100,Color.GREEN,"High:100");
			cyliList.add(cylinder1);
			cyliList.add(cylinder2);
			cyliList.add(cylinder3);
			
			//先设置画布
			CanvasSetting setting = CanvasSetting.getMockInstance();
			setting.setCountOfCylinder(cyliList.size());
			
//			setting.setTopMargin(screenHeigth/10);
//			setting.setBottomMargin(screenHeigth/10);
			
			int canvasWidth = screenWidth - setting.getLeftMargin() - setting.getRightMargin();
			Log.i(Activity_Name, "canvasWidth"+canvasWidth);
			double widthOfCylinder = (canvasWidth - setting.getSpaceInCylis() * (setting.getCountOfCylinder()-1))/setting.getCountOfCylinder();
			setting.setWidthOfCylinder((int)widthOfCylinder);
			Log.i(Activity_Name, "widthOfCylinder"+(int)widthOfCylinder);
			

			
			for(int i=0;i<cyliList.size();i++){
				Cylinder cylinder = cyliList.get(i);
				mPaint.setColor(cylinder.getColor());
				int tlX;
				int tlY;
				int brX;
				int brY;
				
				if(i==0){
					tlX = setting.getLeftMargin();
					tlY = screenHeigth - setting.getBottomMargin() - cylinder.getHeight();
					brX = setting.getLeftMargin() + setting.getWidthOfCylinder();
					brY = screenHeigth - setting.getBottomMargin();
				}else{
					tlX = (int)(setting.getLeftMargin() + i * setting.getWidthOfCylinder() + setting.getSpaceInCylis()* i);
					tlY = screenHeigth - setting.getBottomMargin() - cylinder.getHeight();
					brX = (int)(setting.getLeftMargin() + (i + 1)* setting.getWidthOfCylinder()+ setting.getSpaceInCylis()* i);
					brY = screenHeigth - setting.getBottomMargin();					
				}
				
				mPaint.setTextSize(30);
				
				canvas.drawText(cylinder.getChoiceText(), tlX, tlY-20, mPaint);
				canvas.drawRect(tlX,tlY,brX,brY, mPaint);
//				canvas.drawRect(160, 150, 260, 600, mPaint);
				canvas.save();
			}
			
			// 画矩形
			mPaint.setColor(Color.RED);
			//drawRect(float left, float top, float right, float bottom, Paint paint)
//			canvas.drawRect(50,300,150,600, mPaint);
			//drawText(String text, float x, float y, Paint paint)
			//Draw the text, with origin at (x,y), using the specified paint.
//			canvas.drawText("Low:30", 50, 45, mPaint); 
//			canvas.save();// 每画完一个图做备忘存储,以保证与其它图形互不影响
			
//			mPaint.setColor(Color.YELLOW);
//			canvas.drawRect(160, 150, 260, 600, mPaint);// 画矩形
//			canvas.save();
			
//			mPaint.setColor(Color.BLUE);
//			canvas.drawRect(270, 250, 370, 600, mPaint);// 画矩形
//			canvas.save();
			
			// 画旋转
			//canvas.rotate(45);// 旋转角度45度
//			mPaint.setColor(Color.GREEN);
//			canvas.drawRect(160, 150, 260, 600, mPaint);// 画矩形
//			canvas.save();
			
//			// 画旋转
//			mPaint.setColor(Color.BLUE);
//			canvas.drawRect(200, 50, 100, 400, mPaint);// 画矩形
//			canvas.save();
			
			// 画渐变色圆形
//			Shader mShader = new LinearGradient(0, 0, 100, 100, new int[] {
//					Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW }, null,
//					Shader.TileMode.REPEAT);// 使用着色器
//			mPaint.setShader(mShader);
//			canvas.drawCircle(500, 150, 80, mPaint);
			/*
			 * canvas.save(); //画三角形 mPaint.setStyle(Paint.Style.STROKE);//设置空心
			 * mPaint.setStrokeWidth(3);//设置p外框宽度 Path path=new Path();
			 * path.moveTo(10,330); path.lineTo(70,330); path.lineTo(40,270);
			 * path.close(); canvas.drawPath(path,mPaint); canvas.save(); // 画梯形
			 * mPaint.setAntiAlias(true);//去掉边缘锯齿 mPaint.setColor(Color.BLUE);
			 * mPaint.setStyle(Paint.Style.FILL);//设置实心 Path path1=new Path();
			 * path1.moveTo(10,410); path1.lineTo(70,410); path1.lineTo(55,350);
			 * path1.lineTo(25,350); path1.close();
			 * canvas.drawPath(path1,mPaint); canvas.save(); //画椭圆形
			 * mPaint.setAntiAlias(true);//去掉边缘锯齿 mPaint.setColor(Color.RED);
			 * RectF re=new RectF(10,220,70,250); canvas.drawOval(re,mPaint);
			 */
			canvas.restore();
			mHolder.unlockCanvasAndPost(canvas);// 完成绘图后解锁递交画布视图
		}

		@Override
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
		}
	}
}
